<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

function com_install(){	
	
	$database = JFactory::getDBO();		
	
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_components (
  `id` int(11) NOT NULL auto_increment,
  `component_groupid` mediumtext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();
	
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_items (
  `id` int(11) NOT NULL auto_increment,
  `itemid_groupid` tinytext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();

//take this out for free version
$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_sections (
   `id` int(11) NOT NULL auto_increment,
  `section_groupid` tinytext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();

//take this out for free version
$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_menuaccess (
   `id` int(11) NOT NULL auto_increment,
  `menuid_groupid` tinytext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();

//take this out for free version	
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_categories (
   `id` int(11) NOT NULL auto_increment,
  `category_groupid` tinytext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();

//take this out for free version		
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_modules_two (
   `id` int(11) NOT NULL auto_increment,
  `module_groupid` mediumtext NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();	

	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_usergroups (
	   `id` int(11) NOT NULL auto_increment,
  `name` tinytext NOT NULL,
  `description` text NOT NULL,
  `url` tinytext NOT NULL,
   PRIMARY KEY  (`id`)
	)");
		$database->query();


//check if the default 2 usergroups are already there
$database->setQuery("SELECT id FROM #__fua_usergroups WHERE id='9' LIMIT 1 ");
$defaultgroups = $database -> loadObjectList();
if(count($defaultgroups)>0){
	$defaultgroup = $defaultgroups[0];
	$defaultgroup_id = $defaultgroup->id;
}else{
	$defaultgroup_id = 0;
}

//add url to usergroup table when updating
$database->setQuery("SHOW COLUMNS FROM #__fua_usergroups ");
$columns = $database->loadResultArray();			
if(!in_array('url', $columns)){
	$database->setQuery("ALTER TABLE #__fua_usergroups ADD `url` TINYTEXT NOT NULL ");	
	$database->query();		
}

//if the 2 default usergroups are not already there do insert
if($defaultgroup_id!='9'){
	$database->setQuery("INSERT INTO #__fua_usergroups (`id`, `name`, `description`, `url`) VALUES
	(9, 'logged in', 'all logged in users who have not been assigned to any usergroup',''),
	(10, 'not logged in', 'all users whom are not logged in','')
	");
	$database->query();
}

//check if table userindex exists		
$res = mysql_query("SELECT DATABASE()");
$database_name = mysql_result($res, 0);

$config =& JFactory::getConfig();
$prefix = $config->getValue('dbprefix');

$table_name = $prefix.'fua_userindex';

$res = mysql_query("
		SELECT COUNT(*) AS count 
		FROM information_schema.tables 
		WHERE table_schema = '$database_name' 
		AND table_name = '$table_name'
	");								
	
if(mysql_result($res, 0) == 1){
	//table already exists so alter it
	$database->setQuery("ALTER TABLE #__fua_userindex CHANGE `group_id` `group_id` VARCHAR( 255 ) NOT NULL ");	
	$database->query();
}
		
$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_userindex (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `group_id` VARCHAR( 255 ) NOT NULL,
  PRIMARY KEY  (`id`)
)");
$database->query();

	//do icon
	$database->setQuery("UPDATE #__components SET admin_menu_img='components/com_frontenduseraccess/images/icon.gif' WHERE link='option=com_frontenduseraccess' ");
	$database->query();

//table for configuration
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__fua_config (
  `id` varchar(255) NOT NULL,
  `config` text NOT NULL,
  PRIMARY KEY  (`id`)  
)");
	$database->query();
	
	//check if config is empty, if so insert default config
	$database->setQuery("SELECT id FROM #__fua_config WHERE id='fua' ");
	$rows = $database -> loadObjectList();
	$fua_config = '';
	if(count($rows) > 0){
		$row = $rows[0];
		$fua_config = $row->id;
	}
	
	if($fua_config==''){		
		$configuration = 'default_tab=usergroups
show_joomla_group_select=true
default_usergroup=0
redirect_url=
display_usergroups=true
display_users=true
display_items=true
items_active=
items_reverse_access=
items_message_type=only_text
message_no_item_access_full=
items_display_type=hide
message_no_item_access=
display_categories=true
categories_active=
category_reverse_access=
category_message_type=only_text
message_no_category_access=
display_sections=true
sections_active=
sections_reverse_access=
section_message_type=only_text
message_no_section_access=
display_modules=true
modules_active=
modules_reverse_access=
display_components=true
use_componentaccess=
component_reverse_access=
components_message_type=only_text
message_no_component_access=
use_menuaccess=
menu_reverse_access=
display_menuaccess=true
menuaccess_message_type=only_text
message_no_menu_access=
display_joomla_access_levels=true
docman_workaround=
truncate_article_title=80
no_item_access_full_url=index.php
no_category_access_url=index.php
no_section_access_url=index.php
no_component_access_url=index.php
no_menu_access_url=index.php
';

		//insert fresh config
		$database->setQuery( "INSERT INTO #__fua_config SET id='fua', config='$configuration'");
		$database->query();
	}
	
}

?>
<div style="width: 800px; text-align: left;">
	<h2>Frontend-User-Access</h2>	
	<p>
		Thank you for using Frontend-User-Access.		
	</p>
	<p>
		With Frontend-User-Access you can set frontend access restrictions to:
		<ul>
			<li>components</li>
			<li>modules</li>
			<li>menu-items</li>
			<li>articles (specific articles)</li>
			<li>articles by category</li>
			<li>articles by section</li>
		</ul>
	</p>
	<p>
		You can make as many usergroups as you need and assign users to them. There are 2 predefined usergroups which are very userfull: 
		<ul>
			<li>'not logged in'</li>
			<li>'logged in but not assigned to any usergroup'</li>
		</ul>
	</p>
	<p>
		You can configure for each restriction-type (components, modules, categories, sections etc.) to reverse the access, so any box ticked becomes a restriction instead of an access-right.
	</p>	
	<p>
		Check <a href="http://www.pages-and-items.com" target="_blank">www.pages-and-items.com</a> for:
	<ul>
		<li>updates</li>
		<li>support</li>
		<li>documentation</li>	
		<li>email notification service for updates and new extensions</li>		
	</ul>
	</p>
</div>